package com.zsc.controller.admin;

import com.zsc.dto.DishDTO;
import com.zsc.dto.DishPageQueryDTO;
import com.zsc.entity.Dish;
import com.zsc.result.PageResult;
import com.zsc.result.Result;
import com.zsc.service.DishService;
import com.zsc.utils.CopyUtil;
import com.zsc.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author Enkidu
 * @Date 2024/6/15 21:21
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "管理端 菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询菜品
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品，分页条件：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(long categoryId) {
        List<Dish> list = dishService.listByCategoryId(categoryId);
        return Result.success(list);
    }


    /**
     * 根据菜品id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据菜品id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品 id:{}", id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品，菜品信息：{}", dishDTO);
        Dish dish = CopyUtil.copy(dishDTO, Dish.class);
        dishService.save(dish);
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品，菜品信息：{}", dishDTO);
        Dish dish = CopyUtil.copy(dishDTO, Dish.class);
        dishService.updateById(dish);
        clearCache("dish_*");
        return Result.success();
    }


    /**
     * 菜品 启/停 售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品 启/停 售")
    public Result changeStatus(@PathVariable Integer status, @RequestParam Long id) {
        log.info("改变菜品状态：{}，菜品id为{}", status, id);
        dishService.changeStatus(status, id);
        clearCache("dish_*");
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam Long id) {
        log.info("删除菜品，菜品id为：{}", id);
//        dishService.deleteBatch(ids);
        dishService.removeById(id);
        clearCache("dish_*");
        return Result.success();
    }

    /**
     * 清楚缓存
     *
     * @param pattern
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
